﻿using System;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace OmcSocketClient {

    /// <summary>
    /// Class securing communication with SocketServer
    /// </summary>
    public static class omcSocketClient {

        /// <summary>
        /// Host name of a computer where is running SocketServer
        /// </summary>
        private const string HOST_NAME = "localhost";

        /// <summary>
        /// Specification of port on that will be communicated
        /// </summary>
        private const int PORT = 5555;

        /// <summary>
        /// Method securing data exchange (in string form) with SocketServer
        /// </summary>
        /// <param name="data">Input data for SocketServer (omc)</param>
        /// <returns>Answer from SocketServer (omc)</returns>
        public static string send(string data) {

            TcpClient tcpClient;
            StreamReader fromOMC;
            StreamWriter toOMC;
            String result;

            try {
                //TODO Jirka: tady to pada...
                tcpClient = new TcpClient(HOST_NAME, PORT);
                fromOMC = new StreamReader(tcpClient.GetStream());
                toOMC = new StreamWriter(tcpClient.GetStream());
                toOMC.WriteLine(data);
                toOMC.Flush();
                result = fromOMC.ReadToEnd();
            }
            catch (SocketException ex) {
                result = ex.Message;
            }

            return result;

        }

    }

}
